
#import "DownloadTorrentDetailsViewController.h"

@implementation DownloadTorrentDetailsViewController

//
// torrent info
//
@synthesize torrentInfo														= _torrentInfo;

//
// torrent client
//
@synthesize torrentClient													= _torrentClient;

//
// init
//
- (id)init
{
	//
	// call super
	//
	self																	= [super initWithStyle:UITableViewStyleGrouped];
	if(!self)
		return nil;
	
	//
	// setup title
	//
	self.title																= NSLocalizedStringFromTable(@"Torrent Details", @"DownloadTorrentDetailsViewController", nil);
	self.navigationItem.rightBarButtonItem									= [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:@selector(torrentAction:)] autorelease];
	
	return self;
}

//
// dealloc
//
- (void)dealloc
{	
	self.torrentInfo														= nil;
	self.torrentClient														= nil;
	
	[super dealloc];
}

//
// get section count
//
- (NSInteger)numberOfSectionsInTableView:(UITableView*)tableView
{
	return 1;
}

//
// get row count
//
- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section
{
	return 10;
}

//
// get title
//
- (NSString*)tableView:(UITableView*)tableView titleForHeaderInSection:(NSInteger)section
{
	return self.torrentInfo.torrentName;
}

//
// footer
//
- (NSString*)tableView:(UITableView*)tableView titleForFooterInSection:(NSInteger)section
{
	return self.torrentInfo.message;
}

//
// get cell
//
- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
	UITableViewCell* theCell												= [tableView dequeueReusableCellWithIdentifier:@"v1"];
	if(!theCell)
		theCell																= [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"v1"] autorelease];
	
	switch(indexPath.row)
	{
		//
		// status
		//
	case 0:
		theCell.textLabel.text												= NSLocalizedStringFromTable(@"Status", @"DownloadTorrentDetailsViewController", nil);
		theCell.detailTextLabel.text										= self.torrentInfo.status;
		break;
		
		//
		// size
		//
	case 1:
		theCell.textLabel.text												= NSLocalizedStringFromTable(@"Size", @"DownloadTorrentDetailsViewController", nil);
		theCell.detailTextLabel.text										= [NSString formatSizeRate:self.torrentInfo.size isRate:NO];
		break;
		
		//
		// uploaded size
		//
	case 2:
		theCell.textLabel.text												= NSLocalizedStringFromTable(@"Uploaded", @"DownloadTorrentDetailsViewController", nil);
		theCell.detailTextLabel.text										= [NSString formatSizeRate:self.torrentInfo.uploadedSize isRate:NO];
		break;
		
		//
		// downloaded size
		//
	case 3:
		theCell.textLabel.text												= NSLocalizedStringFromTable(@"Downloaded", @"DownloadTorrentDetailsViewController", nil);
		theCell.detailTextLabel.text										= [NSString stringWithFormat:@"%@ (%.1f%%)", [NSString formatSizeRate:self.torrentInfo.downloadedSize isRate:NO], (float)(self.torrentInfo.downloadedSize * 100.0 / self.torrentInfo.size)];
		break;
		
		//
		// upload rate
		//
	case 4:
		theCell.textLabel.text												= NSLocalizedStringFromTable(@"Upload Rate", @"DownloadTorrentDetailsViewController", nil);
		theCell.detailTextLabel.text										= [NSString formatSizeRate:self.torrentInfo.uploadRate isRate:YES];
		break;
		
		//
		// download rate
		//
	case 5:
		theCell.textLabel.text												= NSLocalizedStringFromTable(@"Download Rate", @"DownloadTorrentDetailsViewController", nil);
		theCell.detailTextLabel.text										= [NSString formatSizeRate:self.torrentInfo.downloadRate isRate:YES];
		break;
		
		//
		// peer count
		//
	case 6:
		theCell.textLabel.text												= NSLocalizedStringFromTable(@"Peer", @"DownloadTorrentDetailsViewController", nil);
		theCell.detailTextLabel.text										= [NSString stringWithFormat:@"%d(%d)", self.torrentInfo.peerCount, self.torrentInfo.totalPeerCount];
		break;
		
		//
		// seeder count
		//
	case 7:
		theCell.textLabel.text												= NSLocalizedStringFromTable(@"Seeder", @"DownloadTorrentDetailsViewController", nil);
		theCell.detailTextLabel.text										= [NSString stringWithFormat:@"%d(%d)", self.torrentInfo.seederCount, self.torrentInfo.totalSeederCount];
		break;
		
		//
		// left time
		//
	case 8:
		theCell.textLabel.text												= NSLocalizedStringFromTable(@"ETA", @"DownloadTorrentDetailsViewController", nil);
		theCell.detailTextLabel.text										= [NSString formatTime:self.torrentInfo.leftTime];
		break;
			
		//
		// share ratio
		//
	case 9:
		theCell.textLabel.text												= NSLocalizedStringFromTable(@"Share Ratio", @"DownloadTorrentDetailsViewController", nil);
		theCell.detailTextLabel.text										= [NSString stringWithFormat:@"%.3f", self.torrentInfo.shareRatio / 1000.f];
		break;
	}
	
	theCell.accessoryType													= UITableViewCellAccessoryNone;
	theCell.selectionStyle													= UITableViewCellSelectionStyleNone;
	return theCell;
}

//
// start torrent
//
- (void)startTorrent
{
	id startTorrentCallback = ^void(id<TorrentClientRequest> startTorrentRequest)
	{
		[ActivityView removeView];
		NSError* error														= [startTorrentRequest error];
		if(error)
		{
			NSString* title													= NSLocalizedStringFromTable(@"Start Torrent Failed", @"DownloadTorrentDetailsViewController", nil);
			[UIAlertView alertViewWithTitle:title message:error.localizedDescription cancel:NSLocalizedString(@"OK", nil)];
		}
		else
		{
			[self.tableView reloadData];
		}
	};
	[ActivityView showActivityView:[NSString stringWithFormat:NSLocalizedStringFromTable(@"Starting %@", @"DownloadTorrentDetailsViewController", nil), self.torrentInfo.torrentName] networking:YES];
	[self.torrentClient startTorrent:self.torrentInfo callback:startTorrentCallback];
}

//
// pause torrent
//
- (void)pauseTorrent
{
	id pauseTorrentCallback = ^void(id<TorrentClientRequest> pauseTorrentRequest)
	{
		[ActivityView removeView];
		NSError* error														= [pauseTorrentRequest error];
		if(error)
		{
			NSString* title													= NSLocalizedStringFromTable(@"Pause Torrent Failed", @"DownloadTorrentDetailsViewController", nil);
			[UIAlertView alertViewWithTitle:title message:error.localizedDescription cancel:NSLocalizedString(@"OK", nil)];
		}
		else
		{
			[self.tableView reloadData];
		}
	};
	[ActivityView showActivityView:[NSString stringWithFormat:NSLocalizedStringFromTable(@"Pausing %@", @"DownloadTorrentDetailsViewController", nil), self.torrentInfo.torrentName] networking:YES];
	[self.torrentClient pauseTorrent:self.torrentInfo callback:pauseTorrentCallback];
}

//
// stop torrent
//
- (void)stopTorrent
{
	id stopTorrentCallback = ^void(id<TorrentClientRequest> stopTorrentRequest)
	{
		[ActivityView removeView];
		NSError* error														= [stopTorrentRequest error];
		if(error)
		{
			NSString* title													= NSLocalizedStringFromTable(@"Stop Torrent Failed", @"DownloadTorrentDetailsViewController", nil);
			[UIAlertView alertViewWithTitle:title message:error.localizedDescription cancel:NSLocalizedString(@"OK", nil)];
		}
		else
		{
			[self.tableView reloadData];
		}
	};
	[ActivityView showActivityView:[NSString stringWithFormat:NSLocalizedStringFromTable(@"Stopping %@", @"DownloadTorrentDetailsViewController", nil), self.torrentInfo.torrentName] networking:YES];
	[self.torrentClient stopTorrent:self.torrentInfo callback:stopTorrentCallback];
}

//
// recheck torrent
//
- (void)recheckTorrent
{
	id recheckTorrentCallback = ^void(id<TorrentClientRequest> recheckTorrentRequest)
	{
		[ActivityView removeView];
		NSError* error														= [recheckTorrentRequest error];
		if(error)
		{
			NSString* title													= NSLocalizedStringFromTable(@"Force Hash Check Failed", @"DownloadTorrentDetailsViewController", nil);
			[UIAlertView alertViewWithTitle:title message:error.localizedDescription cancel:NSLocalizedString(@"OK", nil)];
		}
		else
		{
			[self.tableView reloadData];
		}
	};
	[ActivityView showActivityView:[NSString stringWithFormat:NSLocalizedStringFromTable(@"Forcing Hash Check %@", @"DownloadTorrentDetailsViewController", nil), self.torrentInfo.torrentName] networking:YES];
	[self.torrentClient recheckTorrent:self.torrentInfo callback:recheckTorrentCallback];
}

//
// action
//
- (void)torrentAction:(id)sender
{
	NSString* title															= NSLocalizedStringFromTable(@"Action", @"DownloadTorrentDetailsViewController", nil);
	NSMutableArray* buttons													= [NSMutableArray arrayWithCapacity:4];
	NSString* startTitle													= NSLocalizedStringFromTable(@"Start", @"DownloadTorrentDetailsViewController", nil);
	NSString* pauseTitle													= NSLocalizedStringFromTable(@"Pause", @"DownloadTorrentDetailsViewController", nil);
	NSString* stopTitle														= NSLocalizedStringFromTable(@"Stop", @"DownloadTorrentDetailsViewController", nil);
	NSString* recheckTitle													= NSLocalizedStringFromTable(@"Recheck Hash", @"DownloadTorrentDetailsViewController", nil);
	if(self.torrentInfo.canStart)
		[buttons addObject:startTitle];
	
	if(self.torrentInfo.canPause)
		[buttons addObject:pauseTitle];
	
	if(self.torrentInfo.canStop)
		[buttons addObject:stopTitle];
	
	if(self.torrentInfo.canRecheck)
		[buttons addObject:recheckTitle];
	
	id onDismiss = ^void(NSInteger buttonIndex)
	{
		NSString* title														= [buttons objectAtIndex:buttonIndex];
		if([title isEqualToString:startTitle])
			[self startTorrent];
		else if([title isEqualToString:pauseTitle])
			[self pauseTorrent];
		else if([title isEqualToString:stopTitle])
			[self stopTorrent];
		else if([title isEqualToString:recheckTitle])
			[self recheckTorrent];
	};
	[UIActionSheet actionSheetWithTitle:title destructiveTitle:nil buttons:buttons showInView:sender onDismiss:onDismiss onCancel:nil];
}

@end
